/*
 * Menus.cpp
 *
 *  Created on: Apr 26, 2010
 *      Author: Sequoyha
 */

#include "Menus.h"
#include "Winery.h"
#include "WineryList.h"
#include "Admin.h"
#include "Trip.h"
#include "TripManager.h"

WineryList mainWines;

void MainMenu()
{
	//INIT PRIMARY WINERYLIST
	mainWines.initializeListFromFile();
	int choice;
	vector<int> tripPlan;
	enum Options{LIST = 1, PLAN_TOUR, TAKE_TOUR, ADMIN, QUIT};

	//OUTPUT AND USER SELECTION FOR PROGRAM DIRECTION
	do
	{
		cout << "Straight Ahead Wine Tours Tour Planning System\n\n";
		cout << "What would you like to do today?\n";
		cout << "1 - View a list of the Local Wineries\n2 - Plan a Tour\n";
		cout << "3 - Take a tour\n4 - Administrative options\n5 - Quit\n\n";
		cout << "Please enter a selection(1-5):";

		choice = MenuInput(5);

		switch(choice)
		{
		case LIST:
			mainWines.printList();
			break;
		case PLAN_TOUR:
			tripPlan = PlanTrip();
			break;
		case TAKE_TOUR:
			TakeTrip(tripPlan);
			break;
		case ADMIN:
			AdminVerify();
			break;
		case QUIT:
			break;
		}
	}while(choice!=QUIT);
}

vector<int> PlanTrip()
{
	int option;
	int idWinery[10]={0};		//holds the Winery ID number that is
								//sorted by the closest one.
	enum Selection {SHORT_TRIP = 1, CUSTOM_TRIP, EXIT};
	wineryIn planingTrip;
	Trip plan;
	vector<int> List;

	cout<<"\nPrinting the list of Wineries and their distances "
			"from the Canyon Villa" << endl<<endl;
	planingTrip = GetInfo();
	PrintInfo(planingTrip);

		cout<<"Would you like to: "<<endl;
		cout <<"1.- Create a short trip"<<endl;
		cout <<"2.- Select your own trip"<<endl;
		cout <<"3.- Exit"<<endl<<endl;
		cout <<"Please enter a selection (1-3): ";
		option = MenuInput(3);

		switch(option)
		{
		case SHORT_TRIP:
			cout<<endl;
			plan.shortTrip(planingTrip, idWinery);
			break;
		case CUSTOM_TRIP:
			cout<<"custom"<<endl;
			plan.customTrip(planingTrip, idWinery);
			break;
		default:
			break;
		}
		List = plan.getList();
		return List;

}
/********************************************************************
 * TakeTrip
 *
 * Accesses trip plan and loops through wineries in order. Accesses
 * TripManagement to allow for purchases and sales recording
 *
 * Pre-conditions: plan vector must pre-exist and be passed as
 * argument.
 * Post-conditions: none
 *******************************************************************/
void TakeTrip(vector<int>plan)
{
	TripManager Tour;			//TripManager Object
	vector<int>::iterator pl;	//vector iterator

	//Loop through vector and visit each winery
	for(pl = plan.begin(); pl < plan.end(); pl++)
	{
		//Accessing TripManagement
		Tour.goToWinery(*pl);
		Tour.findWineryInfo();
	}
	//Print out sales information
	Tour.finishTrip();

}
void Splash(){} //Group splash screen for presentation

/*********************************************************************
 * AdminMenu
 *
 * Menu options for administrative procedures
 *
 * Pre-conditions: none
 *
 * Post-conditions: none
 *******************************************************************/
void AdminMenu()
{
	int choice;		//int for making menu selection
	//enumerated menu options for readability
	enum Options{ADD_WINERY = 1, DELETE_WINERY, EDIT_WINE, INIT, EXIT};
	do
	{
		//OUTPUT - Menu options
		cout << "\n\nAdministrative Options\n";
		cout << "1 - Add a New Winery\n";
		cout << "2 - Delete a Winery\n";
		cout << "3 - Edit Wines at a Winery\n";
		cout << "4 - Initialize the Winery List\n";
		cout << "5 - Exit\n";

		cout << "\nPlease make a selection(1-5):";
		//INPUT
		choice = MenuInput(5);

		//Executing options
		switch(choice)
		{
		case ADD_WINERY:
			AddWinery(mainWines);
			break;
		case DELETE_WINERY:
			DeleteWinery(mainWines);
			break;
		case EDIT_WINE:
			EditWinery(mainWines);
			break;
		case INIT:
			mainWines.initializeListFromFile();
			break;
		case EXIT:
			mainWines.writeToBinary("winelist.dat");
			break;
		default:
			break;
		}
	}while(choice != EXIT);
	//clear input buffer to make other inputs later function well
	cin.ignore();
}
/*********************************************************************
 * MenuInput
 *
 * Checks for input of integers and checks for errors against proper
 * input
 *
 * Pre-conditions: maxValue must be declared so the function knows
 * upper boundary
 *
 * Post-conditions: proper input value returned to calling function
 *******************************************************************/
int MenuInput(int maxValue)
{
	int selection;		//integer for input
	//Loop to ensure proper input
	while(!(cin >> selection))
	{
		//Checks if not an integer
		if(!(cin >> selection))
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << setw(5) << " "
					<< "Enter Integer(Whole Number) value only\n";
		}
		//Checks to see if value within range
		else if(selection < 1 || selection > maxValue)
		{
			cout << setw(5) << " "  << "Enter a value between 1 and "
					<< maxValue << " only\n";
		}
		cout <<setw(5) << " " << "Please re-enter a selection: ";
	}
		return selection;
}
/*********************************************************************
 * MenuInput
 *
 * Checks for input of Yes or No and checks for errors against proper
 * input
 *
 * Pre-conditions: none
 *
 * Post-conditions: proper input value returned to calling function
 *******************************************************************/
char MenuInput()
{
	char choice;
	cin >> choice;
	choice = toupper(choice);
	while(choice != 'Y' && choice != 'N')
	{
		cout << setw(5) << " "  << "Enter Y or N only\n";
		cout << "Please re-enter choice(Y or N): ";
		cin >> choice;
		choice = toupper(choice);
	}
	return choice;
}
